package com.peteralbus.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;


/**
 * The type Swagger config.
 * @author PeterAlbus
 * Created on 2022/1/20
 */
//如代码所示，通过@Configuration注解，让Spring来加载该类配置。
//再通过@EnableSwagger2注解来启用Swagger2。
@Configuration
@EnableSwagger2
//再通过createRestApi函数创建Docket的Bean之后，apiInfo()用来创建该Api的基本信息（这些基本信息会展现在文档页面中）.
// select()函数返回一个ApiSelectorBuilder实例用来控制哪些接口暴露给Swagger来展现，
// 本例采用指定扫描的包路径来定义，Swagger会扫描该包下所有Controller定义的API，并产生文档内容（除了被@ApiIgnore指定的请求）
public class SwaggerConfig
{
    /**
     * Create rest api docket.
     *
     * @return the docket
     */
    @Bean
    public Docket createRestApi()
    {
        return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.any())
                .paths(PathSelectors.any()).build();
    }

    private ApiInfo apiInfo()
    {
        return new ApiInfoBuilder()
                .title("EarthQuick API Doc")
                .description("This is a restful api document of EarthQuick.")
                .contact(new Contact("PeterAlbus", "https://www.peteralbus.com/", "wuhongdb@163.com"))
                .version("1.0")
                .build();
    }

}
